// app.js
var util = require('./utils/util.js')
let shangjiOpenId = null
App({
  globalData: {
    isRegistered:null,
    controller: null,
    openId: null,
    getGlobalShareMedia: null,
    userInfo: null,
    pingtai_tag: '报警装置',
    jinbizhi: {
      qiandao: 0,
      zhuce: 5,
      look_ads: 0,
      yaoqing: 10,
    }
  },
  onShow(options) {
    this.getFangwen() // 访问次数方法
  },
  onLaunch(options) {
    // 接收分享好友传参
    if (options.query.shangjiOpenId) {
      shangjiOpenId = options.query.shangjiOpenId
    }
    // 初始化云开发环境
    wx.cloud.init({
      env: 'fen-0gw141a88f35e01a'
    })
    this.getGlobalJInbizhi()
    this.getSystemController()
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage']
    })
    this.getGlobalShareMedia()
  },
  // 访问次数方法
  getFangwen() {
    wx.cloud.callFunction({
      name: 'fangwen',
      data: {
        tag: 'fangwencishu',
        _updateTime: util.formatTime(new Date()).slice(0, 10)
      }
    })
  },
  // 查询用户状态
  selecteUser() {
    return new Promise((resolve, reject) => {
      wx.cloud.callFunction({
          name: 'user',
          data: {
            tag: 'selecteUser',
            _updateTime: util.formatTime(new Date()).slice(0, 10)
          }
        })
        .then(res => {
          if (res.result.length === 1) {
            // 1.用户已经注册，判断禁用状态  为true 则保存注册状态  为false 则叫他滚蛋
            if (res.result[0].status) {
              this.globalData.userInfo = res.result[0]
              resolve(true)
            } else {
              resolve(false)
              this.jingyongShow()
            }
          } else {
            // 直接注册
            resolve(this.getUserProfileModel())
          }
        })
    })
  },
  // 授权弹窗
  getUserProfileModel() {
    // 获取授权
    return new Promise((resolve, reject) => {
      wx.showModal({
        title: '提示',
        content: '授权后才可以继续操作',
        success:res=>{
          if(res.confirm){
            this.getUserProfile().then(res => {
              resolve(res)
            })
          }else{
            resolve(false)
          }
        }
      })
    })
  },
  // 获取登录授权
  getUserProfile() {
    return new Promise((resolve, reject) => {
      wx.getUserProfile({
        desc: '用于完善资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          this.globalData.userInfo = res.userInfo
          this.zhuceUser(res.userInfo).then(res => {
            resolve(res)
          })
        },
        fail: (res) => {
          resolve(false)
          
        }
      })
    })

  },
  // 注册用户
  zhuceUser(userInfo) {
    return new Promise((resolve, reject) => {
      wx.cloud.callFunction({
          name: 'user',
          data: {
            tag: 'zhuceUser',
            avatarUrl: userInfo.avatarUrl,
            city: userInfo.city,
            gender: userInfo.gender,
            nickName: userInfo.nickName,
            province: userInfo.province,
            shangjiOpenId: shangjiOpenId,
            createTime: util.formatTime(new Date()),
            _updateTime: util.formatTime(new Date()).slice(0, 10),
            jinbiNum_zhuce: this.globalData.jinbizhi.zhuce
          }
        })
        .then(res => {
          this.globalData.userInfo._openid = res.result
          wx.showToast({
            icon: 'none',
            title: `金币+${this.globalData.jinbizhi.zhuce}`
          })
          // 给上级金币加10
          if (shangjiOpenId) {
            this.addJinbi(this.globalData.jinbizhi.yaoqing,shangjiOpenId)
          } 
          resolve(true)
        })
    })
  },
  // 加金币操作
  addJinbi(zhi, mubiao) {
    wx.cloud.callFunction({
        name: 'user',
        data: {
          tag: 'addJinbi',
          zhi: zhi,
          mubiao: mubiao
        }
      })
  },
  // 异步获取全局媒体资源及分享转发资源图片及文字
  getGlobalShareMedia() {
    let that = this;
    return new Promise((resolve, reject) => {
      wx.cloud.database().collection('jinbi_page_top_show').get()
        .then(res => {
          that.globalData.getGlobalShareMedia = res.data[0]
          resolve(that.globalData.getGlobalShareMedia)
        })
    })
  },
  // 异步获取全局金币值
  getGlobalJInbizhi() {
    let that = this;
    return new Promise((resolve, reject) => {
      wx.cloud.database().collection('jinbi_zhi').get()
        .then(res => {
          that.globalData.jinbizhi = res.data[0]
          resolve(that.globalData.jinbizhi)
        })
    })
  },
  // 异步获取系统控制变量数据查询
  getSystemController() {
    let that = this;
    return new Promise((resolve, reject) => {
      wx.cloud.database().collection('controller').get()
        .then(res => {
          that.globalData.controller = res.data[0]
          resolve(that.globalData.controller)
        })
    })
  },
  // 定义检查用户禁用状态
  jingyongShow() {
    wx.showModal({
      showCancel: false,
      title: '账号禁用提示',
      content: '请联系管理员处理！',
      cancelColor: 'cancelColor',
      complete: () => {
        wx.exitMiniProgram()
      }
    })
  },

})